//
//  TSAlertViewManager.m
//  BaiduTravel
//
//  Created by neusoft on 8/26/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "TSAlertViewManager.h"
#import "TSAlertView.h"

// alert data
@implementation TSAlert

@synthesize 	alertStyleCode;					
@synthesize 	alertBackgroundImage;
@synthesize     alertTitle;
@synthesize 	alertMessage;
@synthesize     isDismissByClick;
@synthesize     delegate;

@end

@implementation TSAlertViewManager

@synthesize alertView = _alertView;

#define kSubmittingImage0        @"image_loadingball0i.png"
#define kSubmittingImage1        @"image_loadingball1i.png"
#define kSubmittingImage2        @"image_loadingball2i.png"
#define kSubmittingImage3        @"image_loadingball3i.png"
#define kSubmittingImage4        @"image_loadingball4i.png"
#define kSubmittingImage5        @"image_loadingball5i.png"
#define kSubmittingImage6        @"image_loadingball6i.png"
#define kSubmittingImage7        @"image_loadingball7i.png"
#define kSubmittingImage8        @"image_loadingball8i.png"
#define kSubmittingImage9        @"image_loadingball9i.png"
#define kSubmittingImage10       @"image_loadingball10i.png"

#define kImageSubmittedNotes     @"image_check.png"
#define kImageSubmitError        @"image_failureface_small.png"
- (id)init
{
    self=[super init];
	if ( self )
	{
        if (!_alertArrays) {
            _alertArrays = [[NSMutableArray alloc] init];
        }
		return self;
	}
	return nil;
}

- (void) beginToShow:( TSAlert* )alert
{   
    if (!_alertView) {
        _alertView = [[TSAlertView alloc] init];
    }
    _alertView.message = alert.alertMessage;
    _alertView.backgroundImage = alert.alertBackgroundImage;
    _alertView.isDismissByClick = alert.isDismissByClick;
    _alertView.delegate = alert.delegate;
    
    if ( alert.alertStyleCode == TSAlertViewStyleSubmittingCode)
    {
        _loadingImageArrays = [[NSMutableArray alloc] initWithObjects:kSubmittingImage10,
                               kSubmittingImage9,kSubmittingImage8,kSubmittingImage7,
                               kSubmittingImage6,kSubmittingImage5,kSubmittingImage4,
                               kSubmittingImage3,kSubmittingImage2,kSubmittingImage1,nil];
        
//        if ([_loadingImageArrays count] > 0 && [_alertArrays objectAtIndex:0] !=nil) 
//        {
//            _alertView.alertImage.image = [UIImage imageNamed:[_loadingImageArrays objectAtIndex:0]];
//        }
        _submitTimer = [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(displayLoadingImage) userInfo:nil repeats:NO];
    }
    else if( alert.alertStyleCode == TSAlertViewStyleSubmittedCode || alert.alertStyleCode == TSAlertViewStylePhotoSavedCode)
    {
        _alertView.alertImage.image = [UIImage imageNamed:kImageSubmittedNotes];
    }
    else if( alert.alertStyleCode == TSAlertViewStyleNetworkNotReachSubmitErrCode || alert.alertStyleCode == TSAlertViewStyleWithoutLocationCode)
    {
        _alertView.alertImage.image = [UIImage imageNamed:kImageSubmitError];
    }
    else if( alert.alertStyleCode == TSAlertViewStyleFilterCode)
    {
        _alertView.alertImage.image = [UIImage imageNamed:@"image_effect_rotate.png"];
    }
    else 
    {
        _alertView.alertImage.image = nil;
    }

    [_alertView show];
}

- (void)displayLoadingImage
{
    _submitTimer = nil;
    _submitTimer = [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(displayLoadingImage) userInfo:nil repeats:NO];
    _submitImageIndex ++;
//    if (_submitImageIndex >= [_loadingImageArrays count])
//    {
//        _submitImageIndex = _submitImageIndex%[_loadingImageArrays count];
//    }
//    _alertView.alertImage.image = [UIImage imageNamed:[_loadingImageArrays objectAtIndex:_submitImageIndex]];
    if (_submitImageIndex > 3) {
        _submitImageIndex = 0;
    }
    if (_alertView.message != nil) {
        NSRange range = [_alertView.message rangeOfString:@"正在提交"];
        if (range.location != NSNotFound) {
            switch (_submitImageIndex) {
                case 0:{
                    _alertView.message = @"正在提交.";
                    break;
                }
                case 1:{
                    _alertView.message = @"正在提交..";
                    break;
                }
                case 2:{
                    _alertView.message = @"正在提交...";
                    break;
                }
                default:
                    break;
            }
        }
    }
    
}

- (void) showAlert
{
    // Begin to show the alert
    if ([_alertArrays count] > 0 && [_alertArrays objectAtIndex:0] != nil)
    {
        if (!_timer && !_isSubmitting )
        {
            TSAlert *oneAlert = [_alertArrays objectAtIndex:0];
            
            if (oneAlert.alertStyleCode == TSAlertViewStyleSubmittingCode) 
            {
                _isSubmitting = YES;
                _timer = nil;
            }
            else
            {
                _timer = [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(timeOut) userInfo:nil repeats:NO];
            }
            [self beginToShow:oneAlert];
        }
    }
}
    
- (void) addAlertToArray:(TSAlert* )alert
{
    BOOL findResult = NO;
    if (_alertArrays)
    {
        for (TSAlert* alertInArray in _alertArrays)
        {
            if (alertInArray.alertStyleCode== alert.alertStyleCode)
            {
                findResult = YES;
                return;
            }
        }
    }
    // Add the alert to array
    if (!findResult)
    {
        [_alertArrays addObject:alert];
    }

    [self showAlert];

}

- (void)timeOut
{
    _timer = nil;
    if ([_alertArrays count] > 0 && [_alertArrays objectAtIndex:0] != nil)
    {
        if (_alertView) 
        {

            [_alertView dismissWithClickedButtonIndex:0 animated:NO];

        }
        [_alertArrays removeObjectAtIndex:0];
        [self showAlert];
    }
    
}

- (void)cancelSubmit
{
    _isSubmitting = NO;
    if (_submitTimer)
    {
        [_submitTimer invalidate];
        _submitTimer = nil;
    }
    _submitImageIndex = 0;
    [_loadingImageArrays release];
    _loadingImageArrays = nil;
    
    if ([_alertView.message rangeOfString:@"正在提交"].location != NSNotFound||[_alertView.message isEqualToString:@"正在上传头像..."])
    {
        [self timeOut];
    }
}

- (void)dealloc 
{
    if (_timer)
    {
        [_timer invalidate];
        _timer = nil;
    }
    if (_submitTimer)
    {
        [_submitTimer invalidate];
        _submitTimer = nil;
    }
    if (_loadingImageArrays) {
        [_loadingImageArrays release];
        _loadingImageArrays = nil;
    }
    if (_alertView) {
        [_alertView release];
        _alertView = nil;
    }
    [_alertArrays release];
    [super dealloc];
}

@end

